package com.etoak.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.etoak.common.core.vo.PageVO;
import com.etoak.common.web.exception.CustomException;
import com.etoak.entity.Dishes;
import com.etoak.entity.Ingredients;
import com.etoak.mapper.IngredientsMapper;
import com.etoak.service.DishesService;
import com.etoak.service.IngredientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author et2406
 * @since 2024-11-18
 */
@Service
public class IngredientsServiceImpl extends ServiceImpl<IngredientsMapper, Ingredients> implements IngredientsService {


    @Autowired
    DishesService dishesService;

    @Override
    public void add(Ingredients ingredients) {

        if (ObjectUtils.isNotEmpty(getByName(ingredients.getName()))) {
            throw new CustomException("食材重复了");
        }

        ingredients.setCreateTime(DateUtil.now());
        this.save(ingredients);
    }

    @Override
    public PageVO<Ingredients> getList(int pageNum, int pageSize, Ingredients ingredients) {

        PageHelper.startPage(pageNum, pageSize);
        List<Ingredients> list = baseMapper.getList(ingredients);
        PageInfo<Ingredients> info = new PageInfo<>(list);

        return new PageVO<>(info.getPageNum(), info.getPageSize(), list, info.getTotal());
    }

    @Override
    public void update(int id, Ingredients ingredients) {
        /* 验证ID */
        if (ObjectUtil.isEmpty(this.getById(id))) {
            // 延迟交换机 CustomExchange
            throw new CustomException("食材不存在");
        }
        /* 验证Name */
        String name = ingredients.getName();
        if(StringUtils.isNotEmpty(name)){
            Ingredients savedIngrediens = this.getByName(ingredients.getName());
            // 如果食材信息不为空并且 id不相同
            if( ObjectUtils.isNotEmpty(savedIngrediens) && !savedIngrediens.getId().equals(id) ){
                throw new CustomException("食材重复");
            }
        }

        ingredients.setId(id);
        ingredients.setUpdateTime(DateUtil.now());
        this.updateById(ingredients);
    }

    @Override
    public void delete(int id) {

        // 食材是否存在
        Ingredients savedIngredients = this.getById(id);
        if( ObjectUtils.isEmpty(savedIngredients) ){
            throw new CustomException("食材不存在!");
        }

        // 食材有没有被使用
        Dishes dishes = new Dishes();
        dishes.setId(id);
        if(dishesService.ifUsedByDishes(dishes)){
            throw new CustomException("食材已经被使用!!");
        }
        this.removeById(id);
    }


    /**
     *
     * @param type 食材类型
     * @return
     */
    @Override
    public List<Ingredients> listByType(String type) {
       Ingredients ingredients = new Ingredients();
       ingredients.setType(type);
       return baseMapper.getList(ingredients);
    }

    private Ingredients getByName(String ingredientsName) {
        return lambdaQuery().eq(Ingredients::getName, ingredientsName).one();
    }
}
